package com.study.csc.a09threadsafe1;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author csc
 * @Date 2023/10/19 11:30
 * @PackageName:com.study.csc.a09threadsafe1
 * @ClassName: MyThread
 * @Description: TODO
 * @Version 1.0
 */
public class MyThread extends  Thread{


    // static 标志这个类所有的对象,都共享ticket数据
      //static volatile AtomicInteger ticket = new AtomicInteger(0) ; //0 - 99

    static  int ticket = 0; //0 - 99

    //加个锁
    public static  Object LOCK = new Object();
    @Override
    public void run() {

        while (true) {
            //同步代码块
            synchronized (MyThread.class) {
                //如果ticket小于100,执行以下逻辑
                if (ticket< 100) {
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println(getName() + "正在卖第" + ticket + "张票!!!");
                    //票数+1
                    ticket++;
                } else {
                    break;
                }
            }
        }
    }//end


}
